package com.common.sdk.net.download.request.model;

import java.io.File;
import java.util.concurrent.atomic.AtomicInteger;

import com.android.sohu.sdk.common.toolbox.StringUtils;
import com.common.sdk.net.download.util.DownloadFilePathUtils;

public abstract class AbsDownloadInfo {

    /**下载的类型为apk*/
    public static final int APK_TYPE = 1;
    public static final String DOWNLOAD_FILE_EXT = ".apk";// TODO 文件名?
    /**下载类型为apk其他*/
    public static final int OTHER_TYPE = 2;

    /**下载等待  的状态  等待状态 默认*/
    public static final int STATE_WAIT = 0;
    /**下载中 下载的状态    当获取完大小创建完文件后变为下载中*/
    public static final int STATE_DOWNLOADING = 1;
    /**下载暂停  重新开始的时候变为开始状态*/
    public static final int STATE_PAUSE_DOWNLOADING = 2;
    /**下载停止  重新开始的时候停止的状态*/
    public static final int STATE_STOP_DOWNLOADING = 3;
    /**下载完成 下载的状态  */
    public static final int STATE_FINISH = 4;
    /**下载删除 的状态  */
    public static final int STATE_DELETE = 5;

    /**已删除  */
    public static final int STATE_DELETED = 6;

    public AbsDownloadInfo(String downloadPath) {
        // if (downloadPath == null) {
        // throw new NullPointerException("downloadPath is empty");
        // }
        this.downloadPath = downloadPath;
        requestStartTime = System.currentTimeMillis();
        // TODO 这个时间不对啊。不能这么写。重新初始化的时候就不对
    }

    /**唯一标识*/
    protected String key;
    /**文件下载地址*/
    protected String downloadPath;
    /**请求开始时间*/
    private long requestStartTime = 0;
    /**下载的状态*/
    private final AtomicInteger downloadState = new AtomicInteger();
    /**文件路径*/
    private File downloadFilePath;

    /**已经下载的大小*/
    private long alreadyDownloadSize = 0;
    /**文件大小*/
    private volatile long fileSize = 0;
    /**下载速度  单位为B*/
    private volatile float speed = 0;
    /**下载百分比*/
    private volatile int downloadPercentage = 0;

    protected abstract int getType();

    /**
     * 得到唯一标识
     * @return
     */
    public abstract String getKey();

    public abstract String getSuffixName();

    public String getDownloadPath() {
        return downloadPath;
    }

    /**
     * 得到文件路径
     * @return
     */
    public File getDownloadFilePath() {
        if (downloadFilePath == null) {
            String suffixName = getSuffixName();
            if (StringUtils.isNotBlank(suffixName)) {
                if (!suffixName.startsWith(".")) {
                    suffixName = "." + suffixName;
                }
            }
            downloadFilePath = DownloadFilePathUtils.getDownloadFilePath(downloadPath, suffixName);
        }
        return downloadFilePath;
    }

    public long getAlreadyDownloadSize() {
        return alreadyDownloadSize;
    }

    public void setAlreadyDownloadSize(long alreadyDownloadSize) {
        this.alreadyDownloadSize = alreadyDownloadSize;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public int getDownloadPercentage() {
//        if(downloadPercentage == 0){
//            return (int) ((int)(alreadyDownloadSize * 100) / getFileSize());
//        }
        return downloadPercentage;
    }

    public void setDownloadPercentage(int downloadPercentage) {
        this.downloadPercentage = downloadPercentage;
    }

    public int getState() {
        return downloadState.get();
    }

    public void setState(int state) {
        this.downloadState.set(state);
    }

    public long getRequestStartTime() {
        return requestStartTime;
    }

    public void setRequestStartTime(long downloadTime) {
        this.requestStartTime = downloadTime;
    }

    /**
     * 下载等待
     */
    public void waitDownload() {
        downloadState.set(STATE_WAIT);
    }

    /**
     *
     */
    public  boolean isWaitDownload() {
        return downloadState.get() == STATE_WAIT;
    }

    /**
     * 开始下载  有他的地方就应该发送开始回调
     */
    public  void startDownload() {
        downloadState.set(STATE_DOWNLOADING);
    }

    /**
     * 是否开始下载
     */
    public  boolean isStartDownload() {
        return downloadState.get() == STATE_DOWNLOADING;
    }

    /**
     * 暂停下载
     */
    public void pauseDownload() {
        downloadState.set(STATE_PAUSE_DOWNLOADING);
    }

    /**
     * 暂停下载
     * @return
     */
    public boolean isPauseDownload() {
        return downloadState.get() == STATE_PAUSE_DOWNLOADING;
    }

    /**
     * 产生异常停止下载
     */
    public void stopDownload() {
        downloadState.set(STATE_STOP_DOWNLOADING);
    }

    /**
     * 是否停止下载
     * @return
     */
    public boolean isStopDownload() {
        return downloadState.get() == STATE_STOP_DOWNLOADING;
    }

    /**
     * 下载完成
     */
    public void finishDownload() {
        downloadState.set(STATE_FINISH);
    }

    /**
     * 是否下载完成
     */
    public boolean isFinishDownload() {
        return downloadState.get() == STATE_FINISH;
    }

    /**
     * 下载删除
     */
    public void deleteDownload() {
        downloadState.set(STATE_DELETE);
    }

    /**
     *是否是下载删除
     */
    public boolean isDeleteDownload() {
        return downloadState.get() == STATE_DELETE;
    }

    /**
     * 已删除
     */
    public void deletedDownload() {
        downloadState.set(STATE_DELETED);
    }

    /**
     *
     * @return true 是否满足开始下载文件要求，存在数据，通过数据库数据判断为下载中
     */
    public boolean isDownloadingDBState() {
        if (isHasDownloadedSize()/* && threadNum != 0*/) {
            return true;
        }
        return false;
    }

    /**
     * 是否加载过大小  有使用这个判断的，需要改
     * @return false 没有下载  true 下载了
     */
    private synchronized boolean isHasDownloadedSize() {
        if (alreadyDownloadSize == 0) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return super.toString() + ", key:" + key + ", downloadState:" + downloadState + ", downloadPath" + downloadPath
                + ", alreadyDownloadSize:" + alreadyDownloadSize + ", fileSize:" + fileSize + ", speed:" + speed
                + ", downloadPercentage:" + downloadPercentage + ", requestStartTime :" + requestStartTime;
    }

    // @Override
    // public boolean equals(Object o) {
    // if (o instanceof AbsDownloadInfo) {
    // AbsDownloadInfo AbsDownloadInfo = (AbsDownloadInfo) o;
    // if (getKey().equals(AbsDownloadInfo.getKey())) {
    // return true;
    // }
    // }
    // return false;
    // }
    /**
     * key值是否相等，即package_name和version_code相等
     * @return
     */
    public boolean isEqualKey(DownloadInfo info) {
        if (info == null) {
            return false;
        }
        boolean packageEquals = false;
        if (StringUtils.isBlank(getKey())) {
            packageEquals = false;
        } else {
            packageEquals = getKey().equals(info.getKey());
        }
        return packageEquals;
    }

}
